package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.R;
import net.zjitc.entity.Category;
import net.zjitc.entity.Employee;
import net.zjitc.service.CategoryService;
import net.zjitc.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> add(@RequestBody Category category){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }
    @GetMapping("/page")
    public R<IPage<Category>> getPage(@RequestParam("page") int currentPage,
                                      @RequestParam("pageSize") int pageSize){
        log.info("接受到分类请求{}",currentPage+"/","{}",pageSize);
        IPage<Category> page = new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByAsc(Category::getSort);
        categoryService.page(page,wrapper);
        log.info(page.getRecords().toString());
        return R.success(page);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") Long id){
        log.info("删除id为{}",id);
        categoryService.remove(id);
        return R.success("删除菜品成功");
    }
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info(category.toString());
        categoryService.updateById(category);
        return R.success("分类修改成功");
    }
    @GetMapping("/list")
    public R<List<Category>> getList(Integer type){
        log.info("要查询的分类为{}",type);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(type!=null,Category::getType,type);
        wrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
